<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\AppointNum;
use App\Models\Family;
use App\Models\HospitalAppoint;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointStreet;
use App\Models\HospitalAppointType;
use App\Models\Street;
use App\Models\Vaccine;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AppointNumController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $hospital_id = Admin::user()->hospital_id;
        $types = HospitalAppointType::query()->pluck('name','id')->toArray();
        $streets = Street::query()->where('hospital_id',$hospital_id)->pluck('title','id')->toArray();
        $intervals = HospitalAppointInterval::query()->where('hospital_id',$hospital_id)->pluck('start','id')->toArray();
        $vaccines = Vaccine::query()->pluck('name','id')->toArray();
        return Grid::make(new AppointNum(), function (Grid $grid) use($hospital_id,$types,$streets,$intervals,$vaccines) {
           // $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->model()->where('hospital_id',Admin::user()->hospital_id);
            $grid->column('appoint_type', '项目')->using($types);
            $grid->column('street_id')->using($streets);
            $grid->column('project','预约内容')->using($vaccines);
           // $grid->column('hospital_id');
            $grid->column('interval_id','时间段')->using($intervals);
            $grid->column('date');
//            $grid->column('week')->using([
//                '1' => '周一  ',
//                '2' => '周二  ',
//                '3' => '周三  ',
//                '4' => '周四  ',
//                '5' => '周五  ',
//                '6' => '周六  ',
//                '0' => '周日  ',
//            ]);
            //$grid->column('num');
            $grid->column('interval_num');
            $grid->column('project_num');
            $grid->column('project_interval_num');

            $grid->filter(function (Grid\Filter $filter) use($types,$streets,$vaccines) {
                $filter->equal('appoint_type')->select($types);
                $filter->equal('street_id')->select($streets);
                $filter->equal('project')->select($vaccines);
                $filter->equal('date')->date();
                $filter->equal('week')->select([
                    '1' => '周一  ',
                    '2' => '周二  ',
                    '3' => '周三  ',
                    '4' => '周四  ',
                    '5' => '周五  ',
                    '6' => '周六  ',
                    '0' => '周日  ',
                ]);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $hospital_id = Admin::user()->hospital_id;
        $types = HospitalAppointType::query()->pluck('name','id')->toArray();
        $streets = Street::query()->where('hospital_id',$hospital_id)->pluck('title','id')->toArray();
        $intervals = HospitalAppointInterval::query()->where('hospital_id',$hospital_id)->pluck('start','id')->toArray();
        $vaccines = Vaccine::query()->pluck('name','id')->toArray();
        return Show::make($id, new AppointNum(), function (Show $grid)  use($hospital_id,$types,$streets,$intervals,$vaccines)  {
            $grid->field('appoint_type', '项目')->using($types);
            $grid->field('street_id')->using($streets);
            $grid->field('project','预约内容')->using($vaccines);
            // $grid->column('hospital_id');
            $grid->field('interval_id','时间段')->using($intervals);
            $grid->field('date');
            $grid->field('week')->using([
                '1' => '周一  ',
                '2' => '周二  ',
                '3' => '周三  ',
                '4' => '周四  ',
                '5' => '周五  ',
                '6' => '周六  ',
                '0' => '周日  ',
            ]);
            //$grid->column('num');
            $grid->field('interval_num');
            $grid->field('project_num');
            $grid->field('project_interval_num');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppointNum(), function (Form $form) {
            $hospital_id =Admin::user()->hospital_id;
            $types=HospitalAppointType::query()->pluck('name','id')->toArray();
            $street=Street::query()->where('hospital_id',$hospital_id)->pluck('title as name','id')->toArray();

            $form->select('appoint_type')
                ->when(2, function (Form $form) {
                    // 值为1和4时显示文本框
                    $vaccines = Vaccine::query()->pluck('name','id')->toArray();
                    $form->select('project')->options($vaccines);
                })
                ->options($types)->load(['interval_id'], ['/api/appoint_time'])->required();;
            $form->hidden('hospital_id')->value($hospital_id);
            $form->select('interval_id')->required();;
            $form->date('date')->required();;
            //$form->text('week');
            //$form->text('num');
            $form->number('interval_num');
            $form->number('project_num');
            $form->number('project_interval_num');
            $form->select('street_id')->options($street)->rules(function (Form $form) use ($hospital_id) {
                $hospitalAppoint = HospitalAppoint::query()->where([
                    'hospital_id'=>$hospital_id,
                    'type'=>$form->model()->appoint_type,
                ])->first();
                if($hospitalAppoint) {
                    $hospitalStreet = HospitalAppointStreet::query()->where('hospital_appoint_id',$hospitalAppoint->id)->count();
                    // 如果不是编辑状态，则添加字段唯一验证
                    if ($hospitalStreet>0) {
                        return 'required';
                    }
                }

            });;
            $form->saving(function (Form $form) use($hospital_id) {
                $week = date('w',strtotime($form->appoint_date));
                $form->week = $week;
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
